package com.ygq.Impl;

import DO.ChooseQuestionDO;
import DTO.AddChooseDTO;
import DTO.PageChooseQuestionDTO;
import DTO.UpdateChooseQuestionDTO;
import Exceptions.AnswerNoEditException;
import Exceptions.QuestionNumberExistedException;
import VO.ChooseQuestionVO;
import com.ygq.ChooseQuestionService;
import mapper.ChooseQuestionMapper;
import mapper.WorkChooseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.PageResult;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChooseQuestionServiceImpl implements ChooseQuestionService {

    @Autowired
    private ChooseQuestionMapper chooseQuestionMapper;

    @Autowired
    private WorkChooseMapper workChooseMapper;


    /**
     * 选择题分页查询
     * @param pageChooseQuestionDTO
     * @return
     */
    public PageResult page(PageChooseQuestionDTO pageChooseQuestionDTO) {
        PageResult pageResult = new PageResult();
        // 第一次查询 查询总数
        Integer total = chooseQuestionMapper.getTotal(pageChooseQuestionDTO);

        // 第二次查询 查询分页数据
        /*
         * 这里将getCurrentPage作为 (currentPage - 1) * pageSize;
         */
        pageChooseQuestionDTO.setCurrentPage( (pageChooseQuestionDTO.getCurrentPage() - 1) * pageChooseQuestionDTO.getPageSize());
        List<ChooseQuestionVO> list = chooseQuestionMapper.page(pageChooseQuestionDTO);
        pageResult.setTotal(total);
        pageResult.setRecords(list);
        return pageResult;
    }

    /**
     * 添加选择题
     * @param addChooseDTO
     */
    public void addChoose(AddChooseDTO addChooseDTO) {
        String questionNumber = addChooseDTO.getQuestionNumber();
        List<String> list = chooseQuestionMapper.getQuestionNumber();
        for (int i = 0; i < list.size(); i++) {
            // 抛出异常，编号重复
            if (list.get(i).equals(questionNumber)) throw new QuestionNumberExistedException();
        }
        ChooseQuestionDO chooseQuestionDO = new ChooseQuestionDO();
        BeanUtils.copyProperties(addChooseDTO,chooseQuestionDO);
        chooseQuestionDO.setChapter(addChooseDTO.getChapterId());
        chooseQuestionDO.setCreateTime(LocalDateTime.now());
        chooseQuestionMapper.addChoose(chooseQuestionDO);
    }

    /**
     * 修改选择题
     * @param updateChooseQuestionDTO
     */
    public Integer updateChoose(UpdateChooseQuestionDTO updateChooseQuestionDTO) {
        // 第一步，先要判断答案是否发生更改，
        // 第二步，如果答案发生更改，则要判断这个试题是否关联试卷，如果关联，则报出 code 为 512 的 答案因为关联试卷不能修改 的异常
        // 第三步，调用mapper，更改数据


        // 调用mapper 获取该试题的答案
        String answer = chooseQuestionMapper.getAnswerById(updateChooseQuestionDTO.getId());
        // 答案发生更改，第二步操作
        if (!answer.equals(updateChooseQuestionDTO.getAnswer())){
            Integer  k = workChooseMapper.getIsRelevancyWork(updateChooseQuestionDTO.getId());
            if (k>0){
                // 试题表中是否存在这个'试题id  (也就是这个题是否被试卷关联) 抛出 code 为 512 的 答案因为关联试卷不能修改 异常
                return 1;
            }
        }
        // 第三步。调用mapper 修改
        chooseQuestionMapper.updateChoose(updateChooseQuestionDTO);
        return 0;
    }

    /**
     * 删除选择题
     * @param id
     */
    public Integer deleteById(Integer id) {
        Integer  k = workChooseMapper.getIsRelevancyWork(id);
        if (k>0){
            // 试题表中是否存在这个'试题id  (也就是这个题是否被试卷关联) 抛出 code 为 512 的 答案因为关联试卷不能修改 异常
            return 1;
        }
        chooseQuestionMapper.deleteById(id);
        return 0;
    }

}
